using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Business;
using Entity;
public partial class CustomerPage_PlanView : System.Web.UI.Page
{
    eProduct product;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (Request.QueryString["ProductID"] == null)
                return;
            int ProductID = int.Parse(Request.QueryString["ProductID"]);
            product = bProduct.getProductByID(ProductID);
            this.Title = product.producName;
            if (product == null)
                return;

            lbProductName.Text = product.producName;
            lbDescription.Text = product.producDescription;
            lbSecurityDeposit.Text = product.producSecurityDeposit.ToString();

            dlPlan.DataSource = bPlan.getPlanByProductID(ProductID);
            dlPlan.DataBind();

            gvEquipment.DataSource = bProduct_Equipment.getProduct_EquipmentByProductID(ProductID);
            gvEquipment.DataBind();
        }
    }
    protected void dlPlan_ItemDataBound(object sender, DataListItemEventArgs e)
    {

       // if (e.Item.ItemType == ListItemType.Item)
      //  {
            //Response.Write(e.Item.ItemType.ToString());
            DataRowView dv = (DataRowView)e.Item.DataItem;
            int PlanID = (int)dv["planID"];
            DataList dlPlanDetail = (DataList)e.Item.FindControl("dlPlanDetail");
            dlPlanDetail.DataSource = bPlanDetail.getPlanDetailByPlanID(PlanID);
            
            dlPlanDetail.DataBind();
            //Response.Write(dv["planName"].ToString());
      //  }
        //else if (e.Item.ItemType == ListItemType.Header)
        //{
        //    ((Label)e.Item.FindControl("lbDescription")).Text = product.producDescription;
        //    ((Label)e.Item.FindControl("lbName")).Text = product.producName;
        //}
    }
    protected void dlPlanDetail_ItemCommand(object source, DataListCommandEventArgs e)
    {

       // Response.Write(e.CommandArgument.ToString());

        //Response.Write(((Label)e.Item.FindControl("lbPlanDetailName")).Text);
       // Response.Write(((HiddenField)e.Item.FindControl("hfPlanName")).Value);
       // Response.Write(((HiddenField)e.Item.FindControl("hfPlanDetailID")).Value);
        String pr = lbProductName.Text + "--" + ((HiddenField)e.Item.FindControl("hfPlanName")).Value + "--"+((Label)e.Item.FindControl("lbPlanDetailName")).Text;
        //Response.Write(pr);
        DataTable dt_cart;
        int ID=int.Parse(e.CommandArgument.ToString());
        if (Session["Cart"] == null)
        {
            dt_cart = new DataTable();
            dt_cart.Columns.Add("ID", typeof(int));
            dt_cart.Columns.Add("Product", typeof(String));
            dt_cart.Columns.Add("SecurityDeposit", typeof(float));
            dt_cart.Columns.Add("Amount", typeof(int));
            dt_cart.Columns.Add("totalSecurityDeposit", typeof(float));
            DataRow dr = dt_cart.NewRow();
            dr["ID"] = ID;
            dr["Product"] = pr;
            float SecurityDeposit = float.Parse(lbSecurityDeposit.Text);
            dr["SecurityDeposit"] = SecurityDeposit;
            dr["Amount"] = 1;
            dr["totalSecurityDeposit"] = SecurityDeposit;

            dt_cart.Rows.Add(dr);
            Session["Cart"] = dt_cart;
        }
        else
        {
            dt_cart = (DataTable)Session["Cart"];
            bool isNew = true;
            foreach (DataRow dr in dt_cart.Rows)
            {
                if ((int)dr["ID"] == ID)
                {
                    int amount=(int)dr["Amount"] + 1;
                    dr["Amount"] = amount;
                    dr["totalSecurityDeposit"] = amount * ((float)dr["SecurityDeposit"]);
                    isNew = false;
                    break;
                    
                }
            }
            if (isNew)
            {
                DataRow dr = dt_cart.NewRow();
                dr["ID"] = ID;
                dr["Product"] = pr;
                float SecurityDeposit = float.Parse(lbSecurityDeposit.Text);
                dr["SecurityDeposit"] = SecurityDeposit;
                dr["Amount"] = 1;
                dt_cart.Rows.Add(dr);
                dr["totalSecurityDeposit"] = SecurityDeposit;
            }
        }
        Response.Redirect("ViewCart.aspx");
      //  Response.Write(dt_cart.Rows.Count.ToString());
    }
}
